package controller

import (
	"api/app/auth/auto/v1/api"
	"api/app/auth/models"
	"api/libs/errs"
	"gitee.com/dreamwood/ez"
	"gitee.com/dreamwood/ez/server"
)

type PageController struct {
	api.PageApi
}

func init() {

	c := &PageController{}
	c.Prepare()
	c.AddRoute("/save", c.SaveAction)
	c.AddRoute("/list", c.ListAction)
	c.AddRoute("/get", c.GetAction)
	c.AddRoute("/scan", c.ScanAction)
	c.AddRoute("/choice", c.ChoiceAction)
	c.AddRoute("/delete", c.DeleteAction).Add(60, "ULR", c.UpdateLeftRight)
	c.AddRoute("/delete_many", c.DeleteManyAction).Add(60, "ULR", c.UpdateLeftRight)
	c.AddRoute(`/tree`, c.Tree)
	c.AddRoute(`/_reg`, c.RegAction)
	c.AddRoute(`/my`, c.MyPageAction)
	c.AddRoute(`/copy`, c.CopyAction).Add(60, "ULR", c.UpdateLeftRight)
	c.AddRoute(`/edit_many`, c.UpdateMany).Add(60, "ULR", c.UpdateLeftRight)
}

//如果不满意自动生成的方法，请自定义，本文件不会被生成器覆盖，请放心操作
//func (this *PageController) SaveAction(operation *server.Operation) {}
//func (this *PageController) GetAction(operation *server.Operation) {}
//func (this *PageController) ScanAction(operation *server.Operation) {}
//func (this *PageController) ListAction(operation *server.Operation) {}
//func (this *PageController) DeleteAction(operation *server.Operation) {}

func (this *PageController) Tree(operation *server.Operation) {
	root := operation.Get("root").IsInt(0)
	op := this.TakeOver(operation)
	crud := models.NewPageCrud()
	condition := ez.M{}
	if root != 0 {
		rootModel, e := crud.Find(root)
		if op.CheckError(e) {
			return
		}
		condition = ez.M{"l__gt": rootModel.L, "r__lt": rootModel.R}
	}
	full, e := crud.FindBy(condition, []string{"l"}, 0, 0)
	if op.CheckError(e) {
		return
	}
	data := this.createTree(root, full)
	op.ReturnSuccess("OK", data)
}

func (this *PageController) createTree(root int, full []*models.Page) []map[string]interface{} {
	// 获取第一层Root
	finds := make([]*models.Page, 0)
	for _, md := range full {
		if md.ParentId == root {
			finds = append(finds, md)
		}
	}
	re := make([]map[string]interface{}, 0)
	for _, find := range finds {
		cur := make(map[string]interface{})
		// 分类树所需数据结构
		cur["id"] = find.Id
		cur["title"] = find.Name
		// 级联选择器所需数据结构
		cur["value"] = find.Id
		cur["label"] = find.Name
		cur["children"] = this.createTree(find.Id, full)
		re = append(re, cur)
	}
	return re
}

func (c *PageController) UpdateMany(operation *server.Operation) {
	this := c.TakeOver(operation)
	query := struct {
		Ids   []int                  `json:"ids"`
		Model map[string]interface{} `json:"_action"`
	}{}
	e := operation.FillJson(&query)
	if this.Try(e) {
		return
	}
	crud := models.NewPageCrud()
	fields := make([]string, 0)
	for field, _ := range query.Model {
		fields = append(fields, field)
	}
	f := crud.Factory
	f.Selects = fields
	for _, id := range query.Ids {
		toUpdate := new(models.Page)
		err := f.CreateFromObject(query.Model, toUpdate)
		if errs.Try(err) {
			continue
		}
		toUpdate.Id = id
		// 因为一些原因，批量修改不应该触发修改事件
		err = f.Update(toUpdate)
		if errs.Try(err) {
			continue
		}
	}
	this.ReturnSuccess("OK", "")
}

func (c *PageController) UpdateLeftRight(operation *server.Operation) {
	models.PageTreeModel{}.ClearDirtyParent()
	models.PageTreeModel{}.UpdateLeftAndRight(0, 0, 0)
}
